﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Quartz.WebManager.Logic;
using Quartz.WebManager.Model;
using System;
using System.Web.Mvc;

namespace Quartz.WebManager.Web.Controllers
{
    /// <summary>
    /// Provides capacities to edit or delete an exiting job.
    /// </summary>
    public class JobController : Controller
    {
        /// <summary>
        /// Displays the form to edit an exiting job.
        /// </summary>
        /// <param name="name">The name of the job.</param>
        /// <param name="group">The group of the job.</param>
        /// <returns>The view associated with this page.</returns>
        [HttpGet]
        public ActionResult Edit(string name, string group)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            else if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentNullException("group");
            }

            JobKey key = new JobKey(name, group);
            JobDetail model = JobDetailManager.RetrieveOne(key);

            return View(model);
        }

        /// <summary>
        /// Validates and stores the modification of the job.
        /// </summary>
        /// <param name="model">The detail about the job.</param>
        /// <returns>A redirection to the home page; or the form in case of an error.</returns>
        [HttpPost]
        public ActionResult Edit(JobDetail model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            JobKey key = new JobKey(model.Name, model.Group);
            JobDetail current = JobDetailManager.RetrieveOne(key);
            if (current == null)
            {
                throw new ArgumentException("Should be associated to an exiting job", "model");
            }
            else if (model.JobType != current.JobType)
            {
                throw new ArgumentException("The type of job can't be changed", "model");
            }

            // Apply the definition to the model and validates all the properties
            model.Definition = current.Definition;
            foreach (var error in PropertyValidator.Instance.Validate(model))
            {
                string errorKey = string.Format("Properties[{0}]", error.Key);
                ModelState.AddModelError(errorKey, error.Value);
            }

            if (ModelState.IsValid)
            {
                JobDetailManager.SaveOne(model, true);
                return this.RedirectToAction("ViewJobDetail", "Home", key);
            }
            else
            {
                return this.View(model);
            }
        }

        /// <summary>
        /// Removes a specific job.
        /// </summary>
        /// <param name="name">The name of the job.</param>
        /// <param name="group">The group of the job.</param>
        /// <returns>A redirection to home page.</returns>
        [HttpGet]
        public ActionResult Remove(string name, string group)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            else if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentNullException("group");
            }

            JobKey jobKey = new JobKey(name, group);
            JobDetail current = JobDetailManager.RetrieveOne(jobKey);
            if (current == null)
            {
                throw new ArgumentException("The job doesn't exist", "name");
            }

            JobDetailManager.RemoveOne(jobKey);
            return this.RedirectToAction("Index", "Home");
        }
    }
}
